package ntd.util.frame.spring.filter;

import ntd.util.config.common.bo.ResponseBO;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.awt.image.BufferedImage;

/**
 * 返回数据统一处理
 */
@RestControllerAdvice(annotations = RestController.class)
public class GlobalResultDataHandler implements ResponseBodyAdvice {

    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        String name = methodParameter.getDeclaringClass().getName();
        return !(name.contains("springdoc") || name.contains("springfox"));
    }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass,
                                  ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        if(o instanceof ResponseBO || o instanceof ResponseEntity || o instanceof String || o instanceof BufferedImage || o instanceof byte[]){
            return o;
        }
        return new ResponseBO<>(o);
    }

}
